package cn.credit.loan.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.credit.loan.entity.Grant;
import cn.credit.loan.service.grant.GrantService;

/**
 * 发放员控制类
 * @author Administrator
 *
 */
@Controller
public class GantController {
		//日志输出
		private Logger logger = Logger.getLogger(GantController.class);
		
		//注入
		@Autowired
		private GrantService grantService;

		/**
		 * 发放员登录
		 * @param grantName
		 * @param grantPwd
		 * @param model
		 * @return
		 */
		@RequestMapping("/grandologin")
		public ModelAndView login(@RequestParam("adminName") String grantName,
							@RequestParam("adminPwd") String grantPwd,ModelMap model,HttpSession session){
			logger.info("####-------->> 进入登录");
			try {
				Grant grant=grantService.grantlogin(grantName, grantPwd);	
				if (grant!=null) {
					if(!grantPwd.trim().equals(grant.getGrantPwd())){
						model.addAttribute("errorpwd", "密码输入不正确");
						return new ModelAndView("after/login","roleModel",model);
					}
					session.setAttribute("grant",grant);
					session.setAttribute("role",4);
					session.setAttribute("rolelogin", "发放专员");
					return new ModelAndView("after/index","roleModel",model);
				}else{
					model.addAttribute("errorName","发放专员不存在");
				}
			}catch (Exception e) {
				model.addAttribute("syserror","出现错误，请联系管理员");
				logger.info("####------->> error =" +"出现错误，请联系管理员");
				e.printStackTrace();
			}
			return new ModelAndView("after/login","roleModel",model);
		}
		
		
		/**
		 * 发放员退出登录
		 * @return
		 */
		/*
		@RequestMapping("/loginout")
		public String loginout(){
			logger.info("####--------》 你已退出登录");
			return "redirect:/grantlogin";
		}
		*/
		
		/**
		 * 添加发放员
		 * @param grant
		 * @param model
		 * @return
		 */
		@RequestMapping("/addgrant")
		public Object addGrant(Grant grant){
			Map<String,Object> result=new HashMap<String,Object>();
			try {
				if(grantService.grantadd(grant)){
					logger.info("注册发放员");
					return "index";					
				}
				result.put("resultType", "注册成功");
			} catch (Exception e) {
				e.printStackTrace();
			}
			for (String it : result.keySet()) {
				logger.info("####------>>"+it);
			}
			return "index";
		}
		
		
		/**
		 * 修改发放员信息
		 * @param user
		 * @return
		 */
		@RequestMapping(value="/updategrant")
		public Object updateGrant(Grant grant){
			Map<String,Object> resutl=new HashMap<String,Object>();
			
			logger.info(grant.getGrantPwd());
			try {
				if(grantService.grantupdate(grant)){
					return "index";
				}
				resutl.put("returnType", "修改成功");
			} catch (Exception e) {
				logger.info("####------> 出现错误");
				e.printStackTrace();
			}
			return resutl;
		}
		
		/**
		 * 删除发放员
		 * @param grantId
		 * @return
		 */
		public String grantdelete(@RequestParam(value="grantId")Integer grantId){
			
			return null;
		}

}
